// pages/resell-detail/resell-detail.js

import {Shop} from '../../../models/shop'
import {Collect} from '../../../models/collect'
import {Publish} from '../../../models/other'
import {check} from '../../../util/identityCheck'
import distribution from '../../../util/distribution'

const collect = new Collect()
const shop = new Shop()
const publishModel = new Publish()

const app = getApp()
Page({
    data: {
        data: {},
        isLike: false,
        id: '',
        isIphoneX: app.globalData.isIPhoneX,
        hasResumes: false
    },

    onLoad(options) {
        distribution(options)
        if(!check())return
        console.log(options)
        wx.showLoading({
            title: '加载中'
        })
        this.setData({
                id: options.id
            }
        )
        shop.detail({
            id: this.data.id
        }, res => {
            console.log(res);
            wx.hideLoading()
            res.envPic = res.envPic.split(',')
            if (res.auditState != 1 && res.backInfo) {
                wx.showToast({
                    title: res.backInfo,
                    icon: 'none'
                })
            }
            this.setData({
                data: res
            })
        })

        collect.statusCollect({
            relationId: this.data.id
        }, res => {
            this.setData({
                isLike: Boolean(res.collected)
            })
        })
    },

    clickCollect() {
        if(!wx.getStorageSync('userInfo').user_id){
            wx.showToast({
                title: '请先登录',
                icon: 'none'
            })
            wx.navigateTo({
                url: `/pages/login/login`
            })
            return
        }
        this.setData({
            isLike: !this.data.isLike
        })
        if (this.data.isLike) {
            collect.collect({
                relationId: this.data.id,
                type: 4
            }, res => {
                console.log(res)
            })
        } else {
            collect.cancelCollect({
                relationId: this.data.id,
                type: 4
            }, res => {
                console.log(res)
            })
        }
    },

    call() {
      publishModel.callTimes({
        type: 4,
        id: this.data.id
      })
        wx.makePhoneCall({
            phoneNumber: this.data.data.phone
        })
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function (options) {
        let params = {
            shareType: 3,
            relationId: this.data.id
        }
        publishModel.publishShare(params, res => {
            // console.log(res)
            // Toast({duration: 3000, message: res});
        })

        let timestamp = Date.parse(new Date())/1000
        return {
            title: this.data.data.title,
            path: `/pages/resell-detail/resell-detail?id=${this.data.id}&otherId=${wx.getStorageSync('userInfo').user_id}&time=${timestamp}&otherCode=${wx.getStorageSync('shareCode')}`
        }
    }
})
